package com.isoftstone.bank.controller;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.bank.dao.RptMapper;
import com.isoftstone.bank.entity.BOrg;
import com.isoftstone.bank.entity.RptMerchantStatistics;
import com.isoftstone.bank.service.impl.BOrgService;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.util.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 文俊 on 2017/12/18.
 */
@Controller
public class RptMerchantStaticticsController extends BaseController{

    @Autowired
    private RptMapper mapper;
    @Autowired
    private BOrgService service;

    @RequiresPermissions("bms-menu.005.001")
    @RequestMapping("/bank/merchantStatistics")
    public ModelAndView toRpt(){
        return new ModelAndView("/bank/rpt/merchantStatistics");
    }

    @RequestMapping("/bank/rpt/merchantStatistics/getList")
    @ResponseBody
    public Object getList(Page page){
        Tidings<List<RptMerchantStatistics>> tidings = new Tidings<>();
        PageData pd = this.getPageData();
        if(pd.get("orgId") == null || StringUtils.isEmpty(pd.get("orgId")+"")){
            pd.put("orgId",this.getUser().getCorpId());
            BOrg org = service.selectByPrimaryKey(Long.valueOf(this.getUser().getCorpId()));
            pd.put("orgLevel",org.getOrgLevel());
        }
        page.setPd(pd);
        List<RptMerchantStatistics> list= mapper.selectRptMerchantStatisticslistPage(page);
        tidings.setT(list).setPage(page);
        tidings.setStatus(Const.SUCCESS);
        return tidings;
    }

    @RequestMapping("/bank/rpt/merchantStatistics/exportExcel")
    public ModelAndView exportExcel(Page page) {
        PageData pd = this.getPageData();
        if(pd.get("orgId") == null || StringUtils.isEmpty(pd.get("orgId")+"")){
            pd.put("orgId",this.getUser().getCorpId());
            BOrg org = service.selectByPrimaryKey(Long.valueOf(this.getUser().getCorpId()));
            pd.put("orgLevel",org.getOrgLevel());
        }
        page.setPd(pd);
        ModelAndView md = null;
        try {
            List<RptMerchantStatistics> templist = mapper.exportExcelRptMerchant(page);
            Map<String,Object> dataMap = new HashMap<String,Object>();
            List<String> titles = new ArrayList<String>();
            titles.add("单位名称");
            titles.add("机构名称");
            titles.add("缴费用户数");
            titles.add("签约用户数");
            titles.add("交易记录数");
            titles.add("交易总额");
            titles.add("单位状态");
            dataMap.put("titles", titles);
            List<PageData> subList = new ArrayList<PageData>();
            for (RptMerchantStatistics rpt:templist) {
                PageData tmpPd = new PageData();
                tmpPd.put("var1",rpt.getCorpName());
                tmpPd.put("var2",rpt.getOrgName());
                tmpPd.put("var3",rpt.getFeeUserCount()+"");
                tmpPd.put("var4",rpt.getSignUserCount()+"");
                tmpPd.put("var5",rpt.getTranCount()+"");
                tmpPd.put("var6",rpt.getTranAmount()+"");
                tmpPd.put("var7",rpt.getStatus());
                subList.add(tmpPd);
            }
            dataMap.put("varList", subList);
            dataMap.put("name","缴费单位缴费统计");
            dataMap.put("sheetName","缴费单位缴费统计");
            ObjectExcelView erv = new ObjectExcelView();
            md = new ModelAndView(erv,dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md;
    }
}
